/*
 * Clase principal del Compilador para el lenguaje C Minus.
 * 
 * Basado en el proyecto descrito en: 
 * Louden, K. (1997). Compiler Construction: Principles and Practice. Boston: PWS.
 * 
 * @ Project: Compiler
 * @ File Name: Main.java
 * @ Date: 21/09/2008
 */
package cml;

import cml.analyzing.*;
import cml.coding.CodeGenerator;
import cml.parsing.ParsingScanner;
/*
 * Clase principal del Compilador para el lenguaje C Minus.
 * 
 * Basado en el proyecto descrito en: 
 * Louden, K. (1997). Compiler Construction: Principles and Practice. Boston: PWS.
 * 
 * @ Project: Compiler
 * @ File Name: Main.java
 */
import cml.parsing.parser;
import cml.parsing.tree.SynNode;
import cml.scanning.Scanner;

public class Main {
	
	private static String source;
	private static String code;
	private static boolean traceScan = false;
	private static boolean traceParse = false;
	private static boolean traceAnalyze = false;
	private static boolean traceCode = true;
	
	public static void main(String args[]) {
		
		source = "sample2.cml";
		code = source.substring(0, source.length() - 4) + ".txt";
		
		System.out.println("COMPILACION CML: " + source);
		
		Scanner scan = new Scanner(source);
		scan.setTrace(traceScan);
		
		ParsingScanner ps = new ParsingScanner(scan);
		parser p = new parser(ps);
		
		try {
			
			if(traceScan)
				System.out.println("\nTokens: \n");
			p.parse();
			
			if(traceParse) {
				
				System.out.println("\nArbol Sintactico: \n");
				SynNode.printTree(p.getTree());
			}
			
			Analyzer sem = new Analyzer();
			sem.setTrace(traceAnalyze);
			
			if(traceAnalyze)
				System.out.println("\nTablas de Simbolos: \n");
			sem.analyze(p.getTree());
			
			CodeGenerator cgen = new CodeGenerator(code, sem.getTables());
			cgen.setTrace(traceCode);
			
			if(traceCode)
				System.out.println("\nGenerando codigo... \n");
			cgen.generateCode(p.getTree());
			if(traceCode)
				System.out.println("\nCodigo guardado en " + code);
		} 
		catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
